package com.tallysystem.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tallysystem.dao.TallysDao;
import com.tallysystem.daoimpl.TallysDaoImpl;
import com.tallysystem.entity.Tallys;

@WebServlet("/fuzzyServlet")
public class GetAllTallyByFuzzyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TallysDao tallysDao = new TallysDaoImpl();
		//分页
		String page = request.getParameter("page");
		String row = request.getParameter("row");
		int npage = 0;
		int nrow = 0;
		 
		int totalPage = 0;
		if (page == null) {
			npage = 1;
		} else {
			npage = Integer.parseInt(page);
			if (npage < 1)
				npage = 1;
		}

		if (row == null) {
			nrow = 13;
		} else {
			nrow = Integer.parseInt(row);
			int total = tallysDao.count();
			totalPage = (total + nrow - 1) / nrow;
			if (npage > totalPage)
				npage = totalPage;
		}
		// 计算下标
		int pos = (npage - 1) * nrow;
		// 当前页
		request.setAttribute("page", npage);
		
		/*	获取查询内容
		 * 	完善sql语句
		 */
		
		String fuzzymonth = request.getParameter("fuzzymonth");
		String fuzzyday = request.getParameter("fuzzyday");
//		System.out.println(fuzzymonth);
//		System.out.println(fuzzyday);
		StringBuffer sb = new StringBuffer();
		
		//按月份查询
//		if(fuzzymonth!=null && !fuzzymonth.equals("")) {
			sb.append(" month(spendtime)  like '%" + fuzzymonth + "%'");
//		}else {
//			System.out.println("请输入关键词");
//			
//		}
		
		//按日查询
//		if(fuzzyday!=null && !fuzzyday.equals("")) {
			sb.append(" and day(spendtime) like '%" + fuzzyday + "%'");
//		}else {
//			System.out.println("请输入关键词");
//		}		
			
		List<Tallys> pageAndFuzzyLists = tallysDao.findByPageAndFuzzy(pos, nrow, sb);
		if(pageAndFuzzyLists == null) {
			System.out.println("查无此记录");
		}
		request.setAttribute("pageAndFuzzyLists", pageAndFuzzyLists);
		
		
		
		request.getRequestDispatcher("tallyfuzzy.jsp").forward(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
